package com.itheima.config;

import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.mapper.MapperScannerConfigurer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;

import javax.sql.DataSource;

/**
 * @author by itheima
 * @Date 2022/4/27
 * @Description 定义mybatis的核心配置类
 */
//@Configuration
//@MapperScan("com.itheima.dao")
public class MybatisConfig {

    /**
     * 定义会话工厂bena，底层可获取SqlSessionFactory原始对象
     * @param ds
     * @return
     */
    @Bean
    public SqlSessionFactoryBean getSqlSessionFactoryBean(@Autowired DataSource ds){
        SqlSessionFactoryBean factoryBean = new SqlSessionFactoryBean();
        //设置pojo的包扫描
        factoryBean.setTypeAliasesPackage("com.itheima.pojo");
        //设置连接池
        factoryBean.setDataSource(ds);
        return factoryBean;
    }

    /**
     * 定义扫描dao接口的配置bean，底层会自动加载dao接口，并生成代码对象，且把生成的
     * 代理对象自动加入IOC容器
     * @return
     */
    @Bean
    public MapperScannerConfigurer mapperScannerConfigurer(){
        MapperScannerConfigurer msc = new MapperScannerConfigurer();
        //可写，可不写
        msc.setSqlSessionFactoryBeanName("getSqlSessionFactoryBean");
        //设置dao层的接口扫描
        msc.setBasePackage("com.itheima.dao");
        return msc;
    }
}
